import { defineStore } from 'pinia';
import { TOKEN_NAME } from '@/config/global';
import { store, usePermissionStore } from '@/store';
import { getUser } from '@/api/login/login';
import { removeAuthorization, getHeardToken, getHeardXtoken } from '@/api/utils';

const InitUserInfo = {
  info: {},
  roles: ['all'],
};

export const useUserStore = defineStore('user', {
  state: () => ({
    userCompany: {
      logoUrl: '',
      name: '',
    },
    VipLevel: 0, //vip等级
    vipShow: false, //vip权益显示
    roleLevel: 3,
    userLevel: 0,
    imgConcat: 'http://file.wuzhan.art/api/image/',
    token: localStorage.getItem(TOKEN_NAME) || 'main_token', // 默认token不走权限
    userInfo: { ...InitUserInfo },
  }),
  getters: {
    roles: (state) => {
      return state.userInfo?.roles;
    },
  },
  actions: {
    setuserLevel(userInfo: number): void {
      this.userLevel = userInfo;
    },
    setroleLevel(userInfo?: number): void {
      this.roleLevel = userInfo ? userInfo : 3;
    },
    setVipLevel(e: number) {
      //vip等级设置
      this.VipLevel = e ? e : 0;
    },
    setvipShow(e: boolean) {
      this.vipShow = e;
    },
    // async login(userInfo: Record<string, unknown>) {
    //   const mockLogin = async (userInfo: Record<string, unknown>) => {
    //     // 登录请求流程
    //     console.log(userInfo);
    //     // const { account, password } = userInfo;
    //     // if (account !== 'td') {
    //     //   return {
    //     //     code: 401,
    //     //     message: '账号不存在',
    //     //   };
    //     // }
    //     // if (['main_', 'dev_'].indexOf(password) === -1) {
    //     //   return {
    //     //     code: 401,
    //     //     message: '密码错误',
    //     //   };
    //     // }
    //     // const token = {
    //     //   main_: 'main_token',
    //     //   dev_: 'dev_token',
    //     // }[password];
    //     return {
    //       code: 200,
    //       message: '登陆成功',
    //       data: 'main_token',
    //     };
    //   };

    //   const res = await mockLogin(userInfo);
    //   if (res.code === 200) {
    //     this.token = res.data;
    //   } else {
    //     throw res;
    //   }
    // },
    setuserCompanyLogo(url: string) {
      this.userCompany.logoUrl = url;
    },
    setuserCompany(info: any) {
      this.userCompany = info;
    },
    async getUserInfo() {
      // const res = await getUser();
      // this.token = getHeardToken();
      // this.userInfo = {
      //   info: res.data,
      //   roles: ['all'],
      // };
      // this.userLevel = res.data.type;
    },
    reoveInfo() {
      removeAuthorization();
      this.userInfo = { ...InitUserInfo };
    },
    async logout() {
      removeAuthorization();
      this.token = '';
      this.userLevel = 0;
      this.userInfo = { ...InitUserInfo };
    },
    async removeToken() {
      this.token = '';
      removeAuthorization();
    },
  },
  persist: {
    afterRestore: (ctx) => {
      if (ctx.store.roles && ctx.store.roles.length > 0) {
        const permissionStore = usePermissionStore();
        permissionStore.initRoutes(ctx.store.roles);
      }
    },
  },
});

export function getUserStore() {
  return useUserStore(store);
}
